<?php

/**
 * This is the model class for table "pages".
 *
 * The followings are the available columns in table 'pages':
 * @property integer $page_id
 * @property string $name
 * @property string $alias
 * @property string $title
 * @property string $keywords
 * @property string $description
 * @property string $layout
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property PageBlocks[] $pageBlocks
 */
class Page extends CActiveRecord {
    const REGION_HEADER = 1;
    const REGION_CONTENT = 2;
    const REGION_FOOTER = 3;
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Page the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{pages}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, alias, description, status', 'required'),
            array('alias', 'unique'),
            array('status', 'numerical', 'integerOnly' => true),
            array('name, alias, title, keywords, description', 'length', 'max' => 255),
            array('layout', 'length', 'max' => 45),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('page_id, name, alias, title, keywords, description, layout, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'pageBlocks' => array(self::HAS_MANY, 'PageBlock', 'page_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'page_id' => 'Page',
            'name' => 'Name',
            'alias' => 'Alias',
            'title' => 'Title',
            'keywords' => 'Keywords',
            'description' => 'Destiption',
            'layout' => 'Layout',
            'status' => 'Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('page_id', $this->page_id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('alias', $this->alias, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('keywords', $this->keywords, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('layout', $this->layout, true);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function afterDelete() {
        parent::afterDelete();

        //delete all value from page block
        PageBlock::model()->deleteAll('page_id = ' . $this->page_id);
    }

    public function addBlock($block, $region, $status) {
        $model = new PageBlock();
        $model->attributes = array(
            'page_id' => $this->id,
            'block_id' => $block->id,
            'region' => $region,
            'status' => $status
        );
    }

    /**
     *
     * @param type $region
     * @return type
     */
    public function getBlockRegion($region){
        $return = array();
        if($this->pageBlocks != NULL){
            //get
            foreach($this->pageBlocks as $block){
                if($block->region == $region){
                    $return[] = $block;
                }
            }
        }

        return $return;
    }

    /**
     * get front end url
     * @return string
     */
    public function getUrl(){
        return FRONT_SITE_URL . "/page/{$this->alias}";
    }

}